local qiaozhongainu = fk.CreateSkill{
    name = "ym2__qiaozhongainu",
}
Fk:loadTranslationTable{
    ["ym2__qiaozhongainu"] = "鞘中哀怒",
    [":ym2__qiaozhongainu"] = "当你成为牌的目标后，你可以明置所有角色至多共计两张牌，以令此牌无法被响应。发动过此技能的回合内，当最后一张明置牌消失后，你令一名角色回复1点体力。",
    ["#ym2__qiaozhongainu"] = "鞘中哀怒：你可以明置至多两名角色至多共计两张牌；以令%arg无法被响应",
    ["#ym2__qiaozhongainu-choose"] = "鞘中哀怒：明置%dest的%arg张牌",
    ["#ym2__qiaozhongainu-recover"] = "鞘中哀怒：令一名角色回复1点体力",

    ["$ym2__qiaozhongainu1"] = "这些东西……不配我拔剑。",
    ["$ym2__qiaozhongainu2"] = "它不会改变我只是一介普通人的事实，也不会改变别的什么。",
}
local U = require "packages/utility/utility"
local DIY = require "packages/diy_utility/diy_utility"

qiaozhongainu:addEffect(fk.TargetConfirmed, {
    anim_type = "control",
    can_trigger = function (self, event, target, player, data)
        return player == target and player:hasSkill(qiaozhongainu.name)
    end,
    on_cost = function (self, event, target, player, data)
        local room = player.room
        local targets = table.filter(room.alive_players, function (cp)
            return #cp:getCardIds("h") > #DIY.getShownCards(cp)
        end)
        if #targets > 0 and (#targets > 1 or #DIY.getShownCards(targets[1]) > 1) then
            local tos = room:askToChoosePlayers(player, {
                targets = targets,
                max_num = 2,
                min_num = 1,
                skill_name = qiaozhongainu.name,
                prompt = "#ym2__qiaozhongainu:::"..data.card:toLogString()
            })
            if #tos > 0 then
                event:setCostData(self, { tos = tos })
                return true
            end
        end
    end,
    on_use = function (self, event, target, player, data)
        local room = player.room
        local tos = event:getCostData(self).tos
        local n = #tos == 2 and 1 or 2
        for _, to in ipairs(tos) do
            local ids = table.filter(to:getCardIds("h"), function (id)
                return not table.contains(DIY.getShownCards(to), id)
            end)
            if to == player then
                local cards = room:askToCards(player, {
                    include_equip = false,
                    pattern = tostring(Exppattern{ids = ids}),
                    skill_name = qiaozhongainu.name,
                    min_num = 1,
                    max_num = n,
                    prompt = "#ym2__qiaozhongainu-choose::"..to.id..":"..U.ConvertNumber(n)
                })
                DIY.showCards(to, cards)
            else
                local card_data, extra_data, visible_data = {}, {}, {}
                table.insert(card_data, {"$Hand", ids})
                for _, id in ipairs(ids) do
                    if not player:cardVisible(id) then
                        visible_data[tostring(id)] = false
                    end
                end
                if next(visible_data) == nil then visible_data = nil end
                extra_data.visible_data = visible_data
                extra_data.min = 1
                extra_data.max = n
                extra_data.prompt = "#ym2__qiaozhongainu-choose::"..to.id..":"..U.ConvertNumber(n)
                -- extra_data.pattern = tostring(Exppattern{ id = ids })
                local result = room:askToPoxi(player, {
                    poxi_type = "AskForCardsChosen",
                    data = card_data,
                    extra_data = extra_data,
                    cancelable = false,
                })
                DIY.showCards(to, result)
            end
        end
        data.use.disresponsiveList = table.simpleClone(room.players)
    end,
})

qiaozhongainu:addEffect(fk.BeforeCardsMove, {
    can_refresh = function (self, event, target, player, data)
        for _, move in ipairs(data) do
            for _, info in ipairs(move.moveInfo) do
                if Fk:getCardById(info.cardId):getMark(MarkEnum.ShownCards) > 0 then
                    return true
                end
            end
        end
    end,
    on_refresh = function (self, event, target, player, data)
        for _, move in ipairs(data) do
            for _, info in ipairs(move.moveInfo) do
                if Fk:getCardById(info.cardId):getMark(MarkEnum.ShownCards) > 0 then
                    move.extra_data = move.extra_data or {}
                    move.extra_data.ym2__qiaozhongainu = true
                end
            end
        end
    end,
})

qiaozhongainu:addEffect(fk.AfterCardsMove, {
    can_trigger = function (self, event, target, player, data)
        if player:hasSkill(qiaozhongainu.name) and player:usedSkillTimes(qiaozhongainu.name) > 0 and
        not table.find(player.room.alive_players, function (tp)
            return #DIY.getShownCards(tp) > 0
        end) then
            for _, move in ipairs(data) do
                if move.extra_data and move.extra_data.ym2__qiaozhongainu then
                    return true
                end
            end
        end
    end,
    on_cost = function (self, event, target, player, data)
        local room = player.room
        local targets = table.filter(room.alive_players, function(p)
            return p:isWounded()
        end)
        if #targets > 0 then
            local tos = room:askToChoosePlayers(player, {
                max_num = 1,
                min_num = 1,
                targets = targets,
                skill_name = qiaozhongainu.name,
                cancelable = false,
                prompt = "#ym2__qiaozhongainu-recover"
            })
            if #tos > 0 then
                event:setCostData(self, { tos = tos })
                return true
            end
        end
    end,
    on_use = function (self, event, target, player, data)
        local room = player.room
        local to = event:getCostData(self).tos[1]
        room:recover({
            who = to,
            num = 1,
            recoverBy = player,
            skillName = qiaozhongainu.name
        })
    end,
})

return qiaozhongainu